package cn.myweixin.controller;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

 

import cn.myweixin.po.Comment;
import cn.myweixin.po.Course;
import cn.myweixin.po.MarkKey;
import cn.myweixin.po.Notice;
import cn.myweixin.po.NoticourseKey;
import cn.myweixin.po.Trainee;
import cn.myweixin.po.Video;
import cn.myweixin.service.impl.CourseImpl;
import cn.myweixin.service.impl.MarkImpl;
import cn.myweixin.service.impl.NoticeCourseImpl;
import cn.myweixin.service.impl.NoticeImpl;
import cn.myweixin.service.impl.SignImpl;
import cn.myweixin.service.impl.TraineeImpl;

@Controller
@RequestMapping("/exam")
public class ExamController {
	
	@Autowired
	 private TraineeImpl traineeImpl;
	@Autowired
	private NoticeImpl noticeImpl;
	@Autowired
	private  SignImpl    signImpl;
	@Autowired
	private NoticeCourseImpl noticeCourseImpl;
	@Autowired
	private MarkImpl   markImpl;
	 @Autowired
	 private CourseImpl courseImpl;
	 
	@RequestMapping("/listnotice")
	public ModelAndView   listnotice(HttpServletRequest request) throws Exception{
		request.setCharacterEncoding("UTF-8");
		HttpSession session=request.getSession();
		Cookie[] cookies=request.getCookies();
		if(cookies!=null){
		for (Cookie cookie : cookies) {
			if(cookie.getName().equals("vx_openid")){
				    String openid=cookie.getValue();
				    Trainee trainee= traineeImpl.selectByPrimaryKey(openid);
				    ModelAndView modelAndView=new ModelAndView();
				    if(trainee!=null){
				    	session.setAttribute("openid", openid);
						List<Integer> noticeid=signImpl.selectNoticeid( openid);
						modelAndView.addObject("noticeid", noticeid);
						modelAndView.setViewName("exam/test");
				    	return modelAndView;
				    }
				    else{
				    	
				    	return new ModelAndView(new RedirectView("user/register.action"));
				    	
				    }
				}
			}
		}
		
		return new ModelAndView(new RedirectView("user/cookievalidate.action"));
	}
	
	
	
	@RequestMapping(value="/ajaxcourse",method={RequestMethod.POST,RequestMethod.GET} )
	public void ajaxcourse(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="noticeid",required=true) Integer noticeid ) throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		HttpSession session=request.getSession();
		 String openid=(String) session.getAttribute("openid");
		 Notice notice=noticeImpl.selectByPrimaryKey(noticeid);
		 List<NoticourseKey> noticourseKeys=noticeCourseImpl.selectCourse(noticeid);
	      
	      String result="";
	   
	      String cid="";
	      String cname="";
	     
			 for (NoticourseKey noticourseKey : noticourseKeys) {
				cid+=noticourseKey.getcId()+",";
				cname+=noticourseKey.getCourse().getName()+",";	 
			}
			 
			 cid=cid.substring(0,cid.length()-1);
			 cname=cname.substring(0,cname.length()-1);
			 result=cid+";"+cname;
	       PrintWriter out=response.getWriter();
	       out.print(result);
	     
	}
	
	
	
	
	@RequestMapping(value="/ajaxtest",method={RequestMethod.POST,RequestMethod.GET} )
	public void ajaxtest(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="noticeid",required=true) Integer noticeid,@RequestParam(value="courid",required=true) Integer courid ) throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		HttpSession session=request.getSession();
		 String openid=(String) session.getAttribute("openid");
		 Notice notice=noticeImpl.selectByPrimaryKey(noticeid);
		 Course course=courseImpl.selectByPrimaryKey(courid);
		 
	      Date date=new Date();
	      int i=date.compareTo(notice.getEnd());
	      String result="";
	      String flag="";
	    
	      if(i>=0){
			 flag="0";
			 MarkKey markKey=new MarkKey();
			 markKey.setcId(courid);
			 markKey.setNoticeId(noticeid);
			 markKey.setTraineeId(openid);
			 MarkKey markKey1=markImpl.selectByPrimaryKey(markKey);
			 if(markKey1==null){
				 flag="0";
				 result=flag+";"+course.getUrl(); 
			 }
			 else{
				 flag="1";
				 result=flag+";该课程已测试过,请勿重复测试" ; 
			 }
			 
			
	      }
	      else{
	    	  flag="2";
	    	  result=flag+";未到测试时间";
	      }
	      
	      
	      PrintWriter out=response.getWriter();
	      out.print(result);
	      out.flush();
	      out.close();
	}
	
	 
	
	
	
}
